<?php

namespace App\Http\Controllers\Admin;

use App\Http\Common\Lib\Common;
use App\Models\Tag;
use Illuminate\Http\Request;

class TagController extends BaseAdminController
{
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = new Tag();
    }

    /**
     * 列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View|\think\response\View
     */
    public function index(){
        if(\request()->ajax()){
            $list = $this->model->getList(0,[['status','>',-1]]);
            //dd($list);
            return response()->json(['code'=>0,'msg'=>'ok','data'=>$list]);
        }else{
            return view('admin.tag.index');
        }
    }

    /**
     * 添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function add(){
        return view('admin.tag.add');
    }
    /**
     * 保存添加数据
     * @return \Illuminate\Http\JsonResponse
     */
    public function addStore(){
        $input = [
          'name'=>\request()->input('name',''),
            'parent_id'=>\request()->input('parent_id',0),
            'status'=>\request()->input('status',1)
        ];
        $res = $this->model->add($input);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }
    /**
     * 添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function edit(){
        return view('admin.tag.edit');
    }
    /**
     * 保存添加数据
     * @return \Illuminate\Http\JsonResponse
     */
    public function editStore(){
        $input = [
            'id'=>\request()->input('id',0),
            'name'=>\request()->input('name',''),
            'parent_id'=>\request()->input('parent_id',0),
            'status'=>\request()->input('status',1)
        ];
        $res = $this->model->edit($input);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }

    /**
     * 修改状态
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeStatus(){
        $id = \request()->input('id',0);
        $opt = \request()->input('opt','');
        $res = $this->model->changeStatus($id,$opt);
        return response()->json(Common::dataFormat($res['status'],$res['msg']));
    }

    /**
     * 详情
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|\think\Response
     */
    public function detail(){
        $id = \request()->input('id',0);
        $res = $this->model->findOne($id);
        return response(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }

    /**
     * 下拉选项
     * @return \Illuminate\Http\JsonResponse
     */
    public function selectList(){
        $list = $this->model->selectList();
        return response()->json(Common::dataFormat(config('statusInfo.status.success'),config('statusInfo.msg.success'),$list));
    }
}
